package ru.braim.weather_app.services;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import ru.braim.weather_app.dto.region_type.RegionTypeRequestDTO;
import ru.braim.weather_app.dto.region_type.RegionTypeResponseDTO;
import ru.braim.weather_app.models.RegionType;
import ru.braim.weather_app.repositories.RegionRepository;
import ru.braim.weather_app.repositories.RegionTypeRepository;
import ru.braim.weather_app.responses.region_type.RegionTypeResponse;
import ru.braim.weather_app.utils.validators.models.RegionTypeValidator;

@Service
public class RegionTypeService {

    private final RegionTypeRepository regionTypeRepository;
    private final RegionRepository regionRepository;

    public RegionTypeService(RegionTypeRepository regionTypeRepository, RegionRepository regionRepository) {
        this.regionTypeRepository = regionTypeRepository;
        this.regionRepository = regionRepository;
    }

    public RegionTypeResponse createRegionType(RegionTypeRequestDTO regionTypeRequestDTO) {
        RegionType regionType = RegionTypeValidator.getValidRegionType(regionTypeRequestDTO);

        if (regionType == null) {
            return new RegionTypeResponse(null, HttpStatus.BAD_REQUEST);
        }

        if (regionTypeRepository.getRegionTypeByType(regionType.getType()) != null) {
            return new RegionTypeResponse(null, HttpStatus.CONFLICT);
        }

        regionTypeRepository.save(regionType);

        RegionTypeResponseDTO regionTypeResponseDTO = new RegionTypeResponseDTO();

        regionTypeResponseDTO.setId(regionType.getId());
        regionTypeResponseDTO.setType(regionType.getType());

        return new RegionTypeResponse(regionTypeResponseDTO, HttpStatus.CREATED);
    }

    public RegionTypeResponse getRegionTypeById(Long id) {

        if (id <= 0) {
            return new RegionTypeResponse(null, HttpStatus.BAD_REQUEST);
        }

        RegionType regionType = regionTypeRepository.findById(id).orElse(null);
        if (regionType == null) {
            return new RegionTypeResponse(null, HttpStatus.NOT_FOUND);
        }

        RegionTypeResponseDTO regionTypeResponseDTO = new RegionTypeResponseDTO();
        regionTypeResponseDTO.setId(regionType.getId());
        regionTypeResponseDTO.setType(regionType.getType());

        return new RegionTypeResponse(regionTypeResponseDTO, HttpStatus.OK);

    }

    public RegionTypeResponse updateRegionType(Long id, RegionTypeRequestDTO regionTypeRequestDTO)  {

        if (id <= 0) {
            return new RegionTypeResponse(null, HttpStatus.BAD_REQUEST);
        }

        RegionType regionType = RegionTypeValidator.getValidRegionType(regionTypeRequestDTO);

        if (regionType == null) {
            return new RegionTypeResponse(null, HttpStatus.BAD_REQUEST);
        }

        RegionType regionTypeFromRepository = regionTypeRepository.findById(id).orElse(null);
        if (regionTypeFromRepository == null) {
            return new RegionTypeResponse(null, HttpStatus.NOT_FOUND);
        }

        regionTypeFromRepository.setType(regionType.getType());

        if (regionTypeRepository.getRegionTypeByType(regionTypeFromRepository.getType()) != null) {
            return new RegionTypeResponse(null, HttpStatus.CONFLICT);
        }

        regionTypeRepository.save(regionTypeFromRepository);

        RegionTypeResponseDTO regionTypeResponseDTO  = new RegionTypeResponseDTO();

        regionTypeResponseDTO.setId(regionTypeFromRepository.getId());
        regionTypeResponseDTO.setType(regionTypeFromRepository.getType());

        return new RegionTypeResponse(regionTypeResponseDTO, HttpStatus.OK);

    }

    public RegionTypeResponse deleteRegionTypeById(Long id) {

        if (id <= 0) {
            return new RegionTypeResponse(null, HttpStatus.BAD_REQUEST);
        }

        RegionType regionType = regionTypeRepository.findById(id).orElse(null);

        if (regionType == null) {
            return new RegionTypeResponse(null, HttpStatus.NOT_FOUND);
        }


        regionTypeRepository.delete(regionType);

        return new RegionTypeResponse(null, HttpStatus.OK);


    }


}
